package com.macasa.panel.entity.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.ISGBDService;
import com.macasa.panel.entity.api.ISGBDServiceDatabase;
import com.macasa.panel.entity.api.ISGBDServiceUser;

@Entity
@Table(name = "sgbd_service_database")
@SequenceGenerator(name = "SGBDServiceDatabase_SEQ", sequenceName = "SGBDServiceDatabaseIdSeq", initialValue = 1)
@Getter
@Setter
@ToString
public class SGBDServiceDatabase extends AbstractModel<Integer> implements ISGBDServiceDatabase {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= -2491394805823214183L;

	@Id
	@Column(name = "sgbd_service_database", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SGBDServiceDatabase_SEQ")
	private Integer				id;

	@Column(name = "name", nullable = false)
	private String				name;

	@Column(name = "disk_quota", nullable = false)
	private Integer				diskQuota;

	@OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.SGBDServiceUser.class)
	@JoinColumn(name = "fk_owner", nullable = false)
	@ForeignKey(name = "fk_sgbd_service_database_01")
	private ISGBDServiceUser	owner;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.SGBDService.class)
	@JoinColumn(name = "fk_sgbd_service_id", nullable = false)
	private ISGBDService		sgbdService;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof SGBDServiceDatabase)) {
			return false;
		}

		final SGBDServiceDatabase other = CastHelpers.cast(obj, SGBDServiceDatabase.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.name, other.name) && Objects.equal(this.diskQuota, other.diskQuota)
				&& Objects.equal(this.owner, other.owner) && Objects.equal(this.sgbdService, other.sgbdService);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.name, this.diskQuota, this.owner, this.sgbdService);
	}
}
